﻿/**
* @description  Adds a label to the functionality of SimpleButton.
*/
package com.foed.buttons {

	import flash.text.TextFormat;
	import flash.utils.getQualifiedClassName;

	import com.foed.Alignment;
	import com.foed.text.Label;
	import com.foed.graphic.StyleFormat;
	
	public class LabelButton extends SimpleButton {
	
		static public const CLASS:String = getQualifiedClassName(LabelButton);

		private var _label:Label;
		private var _labelText:String;
		private var _hAlign:String;
		private var _vAlign:String;

		/**
		* @description	Initializes component properties.
		*/	
		override protected function init():void {
			super.init();
			_classes.push(LabelButton.CLASS);
			_vAlign = Alignment.MIDDLE;
			_hAlign = Alignment.CENTER;
		}

		/**
		* @description	Creates all necessary nested graphics and components.
		*/	
		override protected function draw():void {
			super.draw();
			_label = new Label();
			_label.hAlign = _hAlign;
			_label.vAlign = _vAlign;
			_label.setSize(width, height);
			_label.setStyleFormatName(getStyleFormat().name);
			_label.text = _labelText;
			_label.initialize();
			addChild(_label);
		}

		/**
		* @description	Handles actions and drawing states for the roll out event.
		*/	
		override protected function rollOutButton():void {
			super.rollOutButton();
			_label.setTextFormat(getStyleFormat().getProperty("textFormat") as TextFormat);
		}
		
		/**
		* @description	Handles actions and drawing states for the roll over event.
		*/	
		override protected function rollOverButton():void {
			super.rollOverButton();
			_label.setTextFormat(getStyleFormat().getProperty("rolloverTextFormat") as TextFormat);
		}
		
		/**
		* @description	Handles actions and drawing states for the press event.
		*/	
		override protected function pressButton():void {
			super.pressButton();
			_label.setTextFormat(getStyleFormat().getProperty("selectedTextFormat") as TextFormat);
		}
					
		/**
		* @description	Sets the width and height of component.
		*
		* @param  pWidth  Width of component.
		* @param  pHeight  Height of component.
		*/	
		override public function setSize(pWidth:Number, pHeight:Number):void {
			super.setSize(pWidth, pHeight);
			if (!_initialized) return; 
			_label.setSize(width, height);
		}

		/**
		* @description	Sets the styleFormat instance to apply to component.
		*
		* @param  pFormat  Instance of StyleFormat to apply to component.
		*/
		override public function setStyleFormat(pStyleFormat:StyleFormat):void {
			super.setStyleFormat(pStyleFormat);
			if (!_initialized) return;
			_label.setStyleFormat(getStyleFormat());
		}
	
		/**
		* @description  Returns the string currently used for the button label.
		*
		* @returns  The button label text string.
		*/
		public function get label():String {
			return _labelText;
		}
	
		/**
		* @description  Sets the string to be used for the button label.
		*
		* @param  pText  The text string for the label.
		*/
		public function set label(pLabel:String):void {
			_labelText = pLabel;
			if (_initialized) _label.text = _labelText;
		}

		/**
		* @description  Returns the horizontal alignment for the label.
		*
		* @returns  The horizontal alignment setting for the button label.
		*/
		public function get hAlign():String {
			return _hAlign;
		}
	
		/**
		* @description  Sets the horizontal alignment for the label.
		*
		* @param  pAlignment  The horizontal alignment setting for the button label.
		*/
		public function set hAlign(pAlign:String):void {
			_hAlign = pAlign;
			if (_initialized) _label.hAlign = _hAlign;
		}
		
		/**
		* @description  Returns the vertical alignment for the label.
		*
		* @returns  The vertical alignment setting for the button label.
		*/
		public function get vAlign():String {
			return _vAlign;
		}
	
		/**
		* @description  Sets the vertical alignment for the label.
		*
		* @param  pAlignment  The vertical alignment setting for the button label.
		*/
		public function set vAlign(pAlign:String):void {
			_vAlign = pAlign;
			if (_initialized) _label.vAlign = _vAlign;
		}

	}

}